home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / internet / amitcp3.0b / src.lha / src / amitcp / sys / queue.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-08  |  6.8 KB  |  278 lines

  1. /*
  2.  * $Id: queue.h,v 1.6 1993/06/04 11:16:15 jraja Exp $
  3.  *
  4.  * Copyright (c) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  5.  *                    Helsinki University of Technology, Finland.
  6.  *                    All rights reserved.
  7.  *
  8.  * HISTORY
  9.  * $Log: queue.h,v $
  10.  * Revision 1.6  1993/06/04  11:16:15  jraja
  11.  * Fixes for first public release.
  12.  *
  13.  * Revision 1.5  1993/05/17  01:02:04  ppessi
  14.  * Changed RCS version
  15.  *
  16.  * Revision 1.4  1993/03/03  18:43:17  jraja
  17.  * Cleanup. Removed some unneeded definitions (The 'old' queue stuff).
  18.  *
  19.  * Revision 1.3  93/03/03  12:33:05  12:33:05  jraja (Jarno Tapio Rajahalme)
  20.  * Fixed comments.
  21.  * 
  22.  * Revision 1.2  93/02/04  18:16:19  18:16:19  jraja (Jarno Tapio Rajahalme)
  23.  * commented out some obsolete definitions.
  24.  * 
  25.  * Revision 1.1  92/11/20  15:42:31  15:42:31  jraja (Jarno Tapio Rajahalme)
  26.  * Initial revision
  27.  * 
  28.  *
  29.  */
  30.  
  31. /* 
  32.  * Mach Operating System
  33.  * Copyright (c) 1992 Carnegie Mellon University
  34.  * All Rights Reserved.
  35.  * 
  36.  * Permission to use, copy, modify and distribute this software and its
  37.  * documentation is hereby granted, provided that both the copyright
  38.  * notice and this permission notice appear in all copies of the
  39.  * software, derivative works or modified versions, and any portions
  40.  * thereof, and that both notices appear in supporting documentation.
  41.  * 
  42.  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  43.  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  44.  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  45.  * 
  46.  * Carnegie Mellon requests users of this software to return to
  47.  * 
  48.  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
  49.  *  School of Computer Science
  50.  *  Carnegie Mellon University
  51.  *  Pittsburgh PA 15213-3890
  52.  * 
  53.  * any improvements or extensions that they make and grant Carnegie Mellon 
  54.  * the rights to redistribute these changes.
  55.  */
  56. /*
  57.  * HISTORY
  58.  * Log:    queue.h,v
  59.  * Revision 2.1  92/04/21  17:17:51  rwd
  60.  * BSDSS
  61.  * 
  62.  *
  63.  */
  64.  
  65. #ifndef    SYS_QUEUE_H
  66. #define    SYS_QUEUE_H
  67.  
  68. /*
  69.  *    Queue of abstract objects.  Queue is maintained
  70.  *    within that object.
  71.  *
  72.  *    Supports fast removal from within the queue.
  73.  *
  74.  *    How to declare a queue of elements of type "foo_t":
  75.  *        In the "*foo_t" type, you must have a field of
  76.  *        type "queue_chain_t" to hold together this queue.
  77.  *        There may be more than one chain through a
  78.  *        "foo_t", for use by different queues.
  79.  *
  80.  *        Declare the queue as a "queue_t" type.
  81.  *
  82.  *        Elements of the queue (of type "foo_t", that is)
  83.  *        are referred to by reference, and cast to type
  84.  *        "queue_entry_t" within this module.
  85.  */
  86.  
  87. /*
  88.  *    A generic doubly-linked list (queue).
  89.  */
  90.  
  91. struct queue_entry {
  92.     struct queue_entry    *next;        /* next element */
  93.     struct queue_entry    *prev;        /* previous element */
  94. };
  95.  
  96. typedef struct queue_entry    *queue_t;
  97. typedef    struct queue_entry    queue_head_t;
  98. typedef    struct queue_entry    queue_chain_t;
  99. typedef    struct queue_entry    *queue_entry_t;
  100.  
  101. /*
  102.  *    enqueue puts "elt" on the "queue".
  103.  *    dequeue returns the first element in the "queue".
  104.  *    remqueue removes the specified "elt" from the specified "queue".
  105.  */
  106.  
  107. #define enqueue(queue,elt)    enqueue_tail(queue, elt)
  108. #define    dequeue(queue)        dequeue_head(queue)
  109.  
  110. void        enqueue_head();
  111. void        enqueue_tail();
  112. queue_entry_t    dequeue_head();
  113. queue_entry_t    dequeue_tail();
  114. void        remqueue();
  115.  
  116. /*
  117.  *    Macro:        queue_init
  118.  *    Function:
  119.  *        Initialize the given queue.
  120.  *    Header:
  121.  *        void queue_init(q)
  122.  *            queue_t        q;    \* MODIFIED *\
  123.  */
  124. #define    queue_init(q)    ((q)->next = (q)->prev = q)
  125.  
  126. /*
  127.  *    Macro:        queue_first
  128.  *    Function:
  129.  *        Returns the first entry in the queue,
  130.  *    Header:
  131.  *        queue_entry_t queue_first(q)
  132.  *            queue_t    q;        \* IN *\
  133.  */
  134. #define    queue_first(q)    ((q)->next)
  135.  
  136. /*
  137.  *    Macro:        queue_next
  138.  *    Header:
  139.  *        queue_entry_t queue_next(qc)
  140.  *            queue_t qc;
  141.  */
  142. #define    queue_next(qc)    ((qc)->next)
  143.  
  144. /*
  145.  *    Macro:        queue_end
  146.  *    Header:
  147.  *        boolean_t queue_end(q, qe)
  148.  *            queue_t q;
  149.  *            queue_entry_t qe;
  150.  */
  151. #define    queue_end(q, qe)    ((q) == (qe))
  152.  
  153. #define    queue_empty(q)        queue_end((q), queue_first(q))
  154.  
  155. /*
  156.  *    Macro:        queue_enter
  157.  *    Header:
  158.  *        void queue_enter(q, elt, type, field)
  159.  *            queue_t q;
  160.  *            <type> elt;
  161.  *            <type> is what's in our queue
  162.  *            <field> is the chain field in (*<type>)
  163.  */
  164. #define queue_enter(head, elt, type, field)            \
  165. {                                 \
  166.     if (queue_empty((head))) {                \
  167.         (head)->next = (queue_entry_t) elt;        \
  168.         (head)->prev = (queue_entry_t) elt;        \
  169.         (elt)->field.next = head;            \
  170.         (elt)->field.prev = head;            \
  171.     }                            \
  172.     else {                            \
  173.         register queue_entry_t prev;            \
  174.                                 \
  175.         prev = (head)->prev;                \
  176.         (elt)->field.prev = prev;            \
  177.         (elt)->field.next = head;            \
  178.         (head)->prev = (queue_entry_t)(elt);        \
  179.         ((type)prev)->field.next = (queue_entry_t)(elt);\
  180.     }                            \
  181. }
  182.  
  183. /*
  184.  *    Macro:        queue_field [internal use only]
  185.  *    Function:
  186.  *        Find the queue_chain_t (or queue_t) for the
  187.  *        given element (thing) in the given queue (head)
  188.  */
  189. #define    queue_field(head, thing, type, field)            \
  190.         (((head) == (thing)) ? (head) : &((type)(thing))->field)
  191.  
  192. /*
  193.  *    Macro:        queue_remove
  194.  *    Header:
  195.  *        void queue_remove(q, qe, type, field)
  196.  *            arguments as in queue_enter
  197.  */
  198. #define    queue_remove(head, elt, type, field)            \
  199. {                                \
  200.     register queue_entry_t    next, prev;            \
  201.                                 \
  202.     next = (elt)->field.next;                \
  203.     prev = (elt)->field.prev;                \
  204.                                 \
  205.     queue_field((head), next, type, field)->prev = prev;    \
  206.     queue_field((head), prev, type, field)->next = next;    \
  207. }
  208.  
  209. /*
  210.  *    Macro:        queue_assign
  211.  */
  212. #define    queue_assign(to, from, type, field)            \
  213. {                                \
  214.     ((type)((from)->prev))->field.next = (to);        \
  215.     ((type)((from)->next))->field.prev = (to);        \
  216.     *to = *from;                        \
  217. }
  218.  
  219. #define    queue_remove_first(h, e, t, f)                \
  220. {                                \
  221.     e = (t) queue_first((h));                \
  222.     queue_remove((h), (e), t, f);                \
  223. }
  224.  
  225. #define    queue_remove_last(h, e, t, f)                \
  226. {                                \
  227.     e = (t) queue_last((h));                \
  228.     queue_remove((h), (e), t, f);                \
  229. }
  230.  
  231. /*
  232.  *    Macro:        queue_enter_first
  233.  *    Header:
  234.  *        void queue_enter_first(q, elt, type, field)
  235.  *            queue_t q;
  236.  *            <type> elt;
  237.  *            <type> is what's in our queue
  238.  *            <field> is the chain field in (*<type>)
  239.  */
  240. #define queue_enter_first(head, elt, type, field)            \
  241. {                                 \
  242.     if (queue_empty((head))) {                \
  243.         (head)->next = (queue_entry_t) elt;        \
  244.         (head)->prev = (queue_entry_t) elt;        \
  245.         (elt)->field.next = head;            \
  246.         (elt)->field.prev = head;            \
  247.     }                            \
  248.     else {                            \
  249.         register queue_entry_t next;            \
  250.                                 \
  251.         next = (head)->next;                \
  252.         (elt)->field.prev = head;            \
  253.         (elt)->field.next = next;            \
  254.         (head)->next = (queue_entry_t)(elt);        \
  255.         ((type)next)->field.prev = (queue_entry_t)(elt);\
  256.     }                            \
  257. }
  258.  
  259. /*
  260.  *    Macro:        queue_last
  261.  *    Function:
  262.  *        Returns the last entry in the queue,
  263.  *    Header:
  264.  *        queue_entry_t queue_last(q)
  265.  *            queue_t    q;        \* IN *\
  266.  */
  267. #define    queue_last(q)    ((q)->prev)
  268.  
  269. /*
  270.  *    Macro:        queue_prev
  271.  *    Header:
  272.  *        queue_entry_t queue_prev(qc)
  273.  *            queue_t qc;
  274.  */
  275. #define    queue_prev(qc)    ((qc)->prev)
  276.  
  277. #endif /* SYS_QUEUE_H */
  278.